package com.chb.test.useThreadsafe_06;

public class MyRunnable implements Runnable{
    int ticket = 0;
    @Override
    public void run() {
        // 循环
        // 同步代码块
        // 判断共享数据是否到了末尾
        while (true) {
            if (setTicket()) break;
        }
    }

    private synchronized boolean setTicket() {
        if(ticket == 100) {
            return true;
        } else {
            try {
                Thread.sleep(30);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            ticket++;
            System.out.println(Thread.currentThread().getName() + "执行了" + ticket + "次");
        }
        return false;
    }
}
